<?php
class ViewController extends Zend_Controller_Action{

	public function init(){
		$this->_helper->layout->setLayout ( 'ground' );
	}


	/**
	 * View page for the team
	 * @return html page for the team
	 */
	public function teamAction(){
		error_reporting(E_ALL);
		$id = $this->_getParam('id');

		// get the team details

		$oTeam = TeamPeer::retrieveByPK($id);

		// get the players
		$oPlayers = TeamPeer::getActivePlayers($id);

		// get the matches
		$oMatches = TeamPeer::getUpcomingMatches($id);

		// get the tournaments
		$oTournaments = TeamPeer::getTournaments($id);

		$this->view->team = $oTeam;
		$this->view->players = $oPlayers;
		$this->view->matches = $oMatches;
		$this->view->tournaments = $oTournaments;



	}
	/**
	 *
	 * view action for the tournament
	 * @return html page for tournaments
	 */
	public function seriesAction(){

		$criteria = new Criteria();
		$criteria->add(TournamentPeer::IS_ACTIVE, 'Y');
		$oSeries  = TournamentPeer::doSelect($criteria);


		$this->view->series = $oSeries;
	}

	public function playerAction(){

		$id = $this->_getParam("id");

		$oPlayer = PlayerPeer::retrieveByPK($id);

		if($oPlayer){

			$teams = PlayerPeer::getTeams($id);

			$this->view->player = $oPlayer;
			$this->view->playerStats = $oPlayer->getRecordBooks();
		}
		$criteria = new Criteria();
		$criteria->add(MediaPeer::OWNER_ID,$id,Criteria::EQUAL);
		$criteria->add(MediaPeer::TYPE,'players',Criteria::EQUAL);
		$mediaRes = MediaPeer::doSelectOne($criteria);
		
		$this->view->mediaRes = $mediaRes;
	}
	
	//function to show all restaurants in slide show model
	public function allrestaurantsAction()
	{
		$criteria = new Criteria();
		$criteria->add(RestaurantPeer::IS_ACTIVE,'Y',Criteria::EQUAL);
		$oRestaurant = RestaurantPeer::doSelect($criteria);
		$this->view->restaurantRes	= $oRestaurant;
	}
	//function to show all restaurants in list
	public function listAction()
	{
		$page = $this->_getParam('page');
		if($page == ''){
			$page = 1;
		}
		$criteria = new Criteria();
		$criteria->add(RestaurantPeer::IS_ACTIVE,'Y',Criteria::EQUAL);
		$oCount = RestaurantPeer::doCount($criteria);
		$criteria->setLimit(5);
		$criteria->setOffset(($page-1)*5);
		$oRestaurant = RestaurantPeer::doSelect($criteria);
		
		$this->view->restaurantRes	= $oRestaurant;
		$this->view->page = $page;
		$this->view->count = $oCount;
	}
	//function to show restaurant information onclick of more info
	public function restaurantAction()
	{
			$restaurantId = $this->_getParam('id');
		
		 //Retrieving data from restaurant    
	  		$oRestaurant 	= RestaurantPeer::retrieveByPK($restaurantId);
	  		$restaurantName = $oRestaurant->getName();
	  		$address		= $oRestaurant->getAddress();
	  		$description    = $oRestaurant->getDescription();
	  		$highlights     = $oRestaurant->getHighlights();
	  		$noOfTables		= $oRestaurant->getNoOfTables();
	  		$noOfScreens    = $oRestaurant->getNoOfScreens();
	  		$noOfPax		= $oRestaurant->getNoOfPax();
	  		
	  		
	  		$phoneNumber    = explode(',',$oRestaurant->getPhoneNumber());
	  		$cityId			= $oRestaurant->getCityId();
	  		$stateId        = $oRestaurant->getStateId();
	  		$area 			= $oRestaurant->getArea();
	  		$contactPerson	= $oRestaurant->getContactPerson();
	  		
	  	 
	  		
	  	 //Retrieving data from offers
	  		$criteria  = new Criteria();
	  		$criteria->add(RestaurantOfferPeer::RESTAURANT_ID,$restaurantId,Criteria::EQUAL);
	  		$criteria->add(RestaurantOfferPeer::END_DATE,date('Y-m-d'),Criteria::GREATER_THAN);
	  		$offerResult = RestaurantOfferPeer::doselect($criteria);
	  		$offerResultCnt	= RestaurantOfferPeer::doCount($criteria);

	  	//Retrieving data from media for image
	 	  $imageCriteria  = new Criteria();
	  	  $imageCriteria->add(MediaPeer::OWNER_ID,$restaurantId,Criteria::EQUAL);
	  	  $imageCriteria->add(MediaPeer::TYPE,'restaurant',Criteria::LIKE);
	  	  $imageCriteria->add(MediaPeer::FILE_TYPE,'image',Criteria::LIKE);
	  	  $imageResult    = MediaPeer::doSelectOne($imageCriteria);
	  	  $imageResultCnt = MediaPeer::doCount($imageCriteria);  		

	  	  
	  	  
	  	 //Retrieving data from media for gallery view
			$criteria = new Criteria();
			$criteria->add(MediaPeer::OWNER_ID,$restaurantId,Criteria::EQUAL);
			$criteria->add(MediaPeer::TYPE,'restaurant',Criteria::LIKE);
			$criteria->add(MediaPeer::FILE_TYPE,'image',Criteria::LIKE);
			$galleryResult = MediaPeer::doSelect($criteria);
			$galleryResCnt = MediaPeer::doCount($criteria);
	  	  
			
		//retrieve restaurant data from restaurant
			
	  	//Sending data for view	
	  		$this->view->id  		= $restaurantId;
	  		$this->view->name 		= $restaurantName;
	  		$this->view->address 	= $address;
	  		$this->view->phoneNumber= $phoneNumber;
	  		$this->view->city       = CityPeer::getCities();
		    $this->view->state      = StatePeer::getStates();
		    $this->view->cityId		= $cityId;
		    $this->view->stateId	= $stateId;	
	  		$this->view->area	 	= $area;
	  		$this->view->contactName= $contactPerson;
	  		$this->view->description= $description;
	  		$this->view->highlights = $highlights;
	  		$this->view->noOfTables = $noOfTables;
	  		$this->view->noOfScreens= $noOfScreens;
	  		$this->view->noOfPax    = $noOfPax; 
	  		
	  		$this->view->offerResult		= $offerResult;
	  		$this->view->offerResCnt		= $offerResultCnt;
	  		$this->view->imageResult        = $imageResult;
	  		$this->view->imageResultCnt     = $imageResultCnt;
	  		$this->view->galleryCnt			= $galleryResCnt;
	  		$this->view->galleryResult      = $galleryResult;
	  		
	}
	
	public function upcomingTournamentsAction(){
	
		$criteria = new Criteria();
		$criteria->add(TournamentPeer::IS_ACTIVE, 'Y');
		
		$tournaments = TournamentPeer::doSelect($criteria);
		
		$this->view->tournaments = $tournaments;
	}
	
	public function upcomingMatchesAction(){
	
		$criteria = new Criteria();
		$criteria->add(TournamentPeer::IS_ACTIVE, 'Y');
	    $criteria->add(MatchSchedulePeer::MATCH_DATE, date('Y-m-d'), Criteria::GREATER_EQUAL);
		$matches = MatchSchedulePeer::doSelect($criteria);
		
		$this->view->matches = $matches;
	}
	
	public function teamStandingAction()
	{
		$criteria = new Criteria();
		$criteria->add(UserTeamPeer::IS_ACTIVE,'Y');
		$criteria->add(UserTeamPeer::CURRENT_RANKING,NULL,Criteria::NOT_EQUAL);
		$criteria->addAscendingOrderByColumn(UserTeamPeer::CURRENT_RANKING);
		$criteria->setLimit(5);
		$userTeam = UserTeamPeer::doSelect($criteria);
		
		$this->view->userTeam = $userTeam;
	}
	public function worldcupStandingAction()
	{
		$criteria = new Criteria();
		$criteria->add(TournamentToTeamPeer::GROUP,1,Criteria::EQUAL);
		$criteria->addJoin(TournamentToTeamPeer::TEAM_ID,TeamPeer::ID,Criteria::INNER_JOIN);
		$oGroupA = TeamPeer::doSelect($criteria);
		$this->view->groupA = $oGroupA;
		
		
		
		$criteria = new Criteria();
		$criteria->add(TournamentToTeamPeer::GROUP,2,Criteria::EQUAL);
		$criteria->addJoin(TournamentToTeamPeer::TEAM_ID,TeamPeer::ID,Criteria::INNER_JOIN);
		$oGroupB = TeamPeer::doSelect($criteria);
		$this->view->groupB = $oGroupB;
		
	}
}